<?php
require("../connection.php");
	if(isset($_GET['checkforcustomer']))
	{
		$sql = "SELECT id FROM user WHERE id = :id";
		$sth = $db->prepare($sql);
		$sth->bindParam(":id",$_GET['customerid']);
		$sth->execute();
		$row = $sth->fetch();
		
		$sql2 = "SELECT * FROM requests WHERE uid = :id AND completed='yes'";
		$sth2 = $db->prepare($sql2);
		$sth2->bindParam(":id",$_GET['customerid']);
		$sth2->execute();
		$row2 = $sth2->fetch();
		if($sth2->rowCount()==1)
		{
			echo '<form>
			<select name="rating" id="rating">
			<option>Rating</option>
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
			<option value="4">4</option>
			<option value="5">5</option>
			</select><br />
			<textarea name="comment" id="comment" rows="5" cols="40" placeholder="Legg ved en liten ond, stygg og djevelsk kommentar :)">
			</textarea><br />
			<input type="button" onclick="send(this.form, '.$row["id"].');" value="Send"/><em>Når dette er sendt kan du ikke gjøre endringer</em>
			</form>';
		}
		
	}
	elseif(isset($_GET['submit']))
	{
		$id 	 = $_GET['id']; //Kundens id
		$rating  = $_GET['rating']; //kundens rating på utvikler
		$comment = $_GET['comment']; //Kundens kommentar om utvikler
		
		//finner utvikleren som er ansatt
		$sql = "SELECT * FROM proposition WHERE hired = 'yes'";
		$sth = $db->prepare($sql);
		$sth->execute();
		$row = $sth->fetch();
		$rid = $row['rid']; //anbuds id
		$uid = $row['uid']; //utvikler id
		
		//Velger fra requests hvor utvikler er registrert og completed = yes
		$sql1 = "SELECT * FROM requests WHERE id = :rid AND completed = 'yes'";
		$sth1 = $db->prepare($sql1);
		$sth1->bindParam(":rid",$rid);
		$sth1->execute();
		
		if($sth1->rowCount()==1) //Dersom request er ferdigstillt
		{
			$sql = "INSERT INTO rating (rated, uid) VALUES (:rating, :uid)";
			$sth = $db->prepare($sql);
			$sth->bindParam(":rating",$rating);
			$sth->bindParam(":uid",$uid);
			$sth->execute();
			
			$sql = "INSERT INTO comments (content, date, uid) VALUES (:comment, NOW(),:uid)";
			$sth = $db->prepare($sql);
			$sth->bindParam(":comment",$comment);
			$sth->bindParam(":uid",$uid);
			$sth->execute();
			
			echo "Rating er gitt";
		}
		else
		{
			echo "Ingen anbud er ferdigstillt";
		}
		
	
	}


?>